
#include <BSource>
#include "member_BSource.h"
#include "member_BScript.h"

using namespace BWE;

#define member					(*(member_BSource*)_ptr)
#define member_allocate()		_ptr = new member_BSource(this)
#define member_release()		delete (member_BSource*)_ptr

BSource::BSource()
{
	member_allocate();
}
BSource::BSource(const BString& name) : BObject(name)
{
	member_allocate();
}
BSource::~BSource()
{
	member_release();
}

void BSource::setText(const BString& text)
{
	if (member.text != text)
	{
		member.text = text;
	}
}
const BString& BSource::text() const
{
	return member.text;
}

void BSource::dirty() const
{
	if (!member.dirty)
	{
		member.dirty = true;
	}
}
bool BSource::isdirty() const
{
	return member.dirty;
}

const BExpHolderArray& BSource::analyse(const BString& text)
{
	member.dirty = true;
	member.error = false;
	member.declares.clear();
	member.functionMap.clear();
	member.classMap.clear();
	member.document.clear();
	if (!member.script)
		return member.document;
	if (text.size())
	{
		member.stack.clear();
		member.srcnames.clear();
		member.srcnames += this->name();
		int pos = 0;
		int line = 0;
		while (pos < text.size())
		{
			BExpHolder exp = member.compile(text, pos, line);
			if (exp.empty())
				continue;
			member.checkExp(exp);
		}
		member.srcnames -= this->name();
		member.stack.clear();
	}
	return member.document;
}

const BClass* BSource::findClass(const BString& type)
{
	if (BClass* cls = member.classMap(type))
		return cls;
	if (member.script)
		return  member.script->findClass(type);
	return 0;
}

BScript* BSource::script()
{
	return member.script;
}
const BScript* BSource::script() const
{
	return member.script;
}
